package com.example.mykotlinapp

/**
 *
 * 符号 ：： 的使用
 *
 * @author zhudeshuai
 * @since 2019/7/4
 */

data class Person2(val name: String, val age: Int)

private fun printMessageWithPrefix(message: Collection<String>, prefix: String) {
    message.forEach {
        println("$prefix$it")
    }
}

//成员引用

private fun test70() {
    val getAge2 = { person: Person -> person.age }
    val getAge = Person::age//与上面等价
    val people = arrayListOf(Person2("jack", 12), Person2("tom", 18))
    println(people.maxBy(Person2::age))
}

//引用顶层函数
private fun test71() {
    fun salute() = println("引用顶层函数")
    run(::test70)
}

//构造方法方法引用
private fun test72() {
    val createInstance = ::Person2
    val person01 = createInstance("Alice", 18)
    val person02 = createInstance("Bob", 20)
    println(person01)
    println(person02)
}
//引用扩展函数

fun Person2.isAdult() = age > 19
private fun test73() {
    val predicate = Person2::isAdult
    println(predicate(Person2("Jack", 21)))
    val person = Person2("Tom", 17)
    //捕捉特定实例上引用
    val predicate2 = person::isAdult
    println(predicate2())
}

//集合的函数式api

private fun test74() {
    //filter
    val list = listOf(1, 2, 3, 4)
    println(list.filter { it % 2 == 0 })

    //map
    println(list.map { it * it })
}

//打印出年龄超过20岁的人的名字
private fun test75() {
    val people = listOf(Person2("A", 29), Person2("B", 19), Person2("C", 25))
    println(people.filter { it.age > 20 }.map(Person2::name))

}

private fun test76() {
    val people = listOf(Person2("A", 29), Person2("B", 19), Person2("C", 25))
    println(people.all { it.age < 30 })
    println(people.any { it.age < 20 })
    println(people.count { it.age < 20 })
    println(people.groupBy { it.age })
}


private fun main() {
    test76()
}
